package zj.it.bhne.gridengineeringsurvey.fragment.details;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import zj.it.bhne.gridengineeringsurvey.MyApplication;
import zj.it.bhne.gridengineeringsurvey.R;
import zj.it.bhne.gridengineeringsurvey.activity.LineActivity;
import zj.it.bhne.gridengineeringsurvey.entity.BomBean;
import zj.it.bhne.gridengineeringsurvey.entity.MaterialsProject;
import zj.it.bhne.gridengineeringsurvey.entity.ModelType;
import zj.it.bhne.gridengineeringsurvey.entity.ModuleMaterials;
import zj.it.bhne.gridengineeringsurvey.entity.greendao.Line;
import zj.it.bhne.gridengineeringsurvey.myutils.greendao.LineDao;

import static zj.it.bhne.gridengineeringsurvey.myutils.MyUtils.openDatabase;

/**
 * 材料统计
 */
public class MaterialFragment extends Fragment {
    List<Line> line;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_material, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        //TODO 1.查所有的线然后进行
        line = MyApplication.getInstances().getLineDao().queryBuilder().build().list();
        for (int i = 0; i < line.size(); i++) {
            Log.e("id", line.get(i).getModuleID() + "");
            Log.e("name", line.get(i).getModuleTxt() + "");
        }
        List<MaterialsProject> result666 = getReulst(line);
        List<BomBean> bomBeanresult = new ArrayList<>();
        Log.e("666", result666.toString());
        for (int i = 0; i < result666.size(); i++) {
            BomBean bomBean = new BomBean();
            bomBean.setMaterialCodeERP(result666.get(i).getMaterialCodeERP());
            bomBean.setMaterialName(result666.get(i).getMaterialName());
            bomBean.setSpec(result666.get(i).getSpec());
            bomBean.setERPUnit(result666.get(i).getERPUnit());
            //TODO 这里搞数据计算
           // eval("10*1.1");
            //bomBean.setNum(materialsProjects.get(i).get());
            bomBean.setTechnicalProtocol(result666.get(i).getTechnicalProtocol());
            //TODO 类型
            //bomBean.setBomType("类型");

            Log.e("modelID", result666.get(i).getMaterialsProjectID());
            //TODO  这里去计算结果
            List<Line> L = MyApplication.getInstances().getLineDao().queryBuilder().where(LineDao.Properties.ModuleID.eq(result666.get(i).getMaterialsProjectID())).build().list();
            if (L != null && L.size() > 0) {
                Log.e("lll=",L.get(0).getLength());
                Log.e("gongshi",result666.get(i).getWuliaoFormnue());
                String gongshi = result666.get(i).getWuliaoFormnue().replace("L", L.get(0).getLength().trim());
                Log.e("resl", gongshi);
                if (gongshi.contains("+") || gongshi.contains("-") || gongshi.contains("*") || gongshi.contains("/")) {
                    Log.e("L=", eval(gongshi.trim()) + "");
                    bomBean.setNum(eval(gongshi.trim()) + "");
                    // Log.e("L=", eval(gongshi) + "");
                } else {
                    Log.e("L=", eval(gongshi.trim()+"*"+L.get(0).getLength().trim()) + "");
                    bomBean.setNum(eval(gongshi.trim()+"*"+L.get(0).getLength().trim()) + "");

                }
            } else {
                Log.e("L=", "-");
                bomBean.setNum("");

            }
            bomBeanresult.add(bomBean);
        }
       /* Log.e("哈哈:",bomBeanresult.toString());
        [BomBean{MaterialCodeERP='500014663', MaterialName='架空绝缘导线', Spec='AC10kV,JKLYJ,240', ERPUnit='千米', num='', TechnicalProtocol='9906-500059246-00006', BomType=''},
        BomBean{MaterialCodeERP='500132455', MaterialName='低压电力电缆', Spec='低压电力电缆,YJLV,铝,70,4芯,ZC,无铠装,普通', ERPUnit='千米', num='73.24900000000001', TechnicalProtocol='9906-500108894-00001', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='异型并沟线夹', Spec='异型并沟线夹', ERPUnit='只', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='500021801', MaterialName='电缆接线端子', Spec='电缆接线端子,铜铝过渡,70mm2,单孔', ERPUnit='只', num='', TechnicalProtocol='24GH-500021801-00002', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×70,户外终端,冷缩,铝', ERPUnit='副', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='500033187', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×70,户内终端,冷缩,铝', ERPUnit='副', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='成套抱箍', Spec='成套抱箍,—8×80,D200', ERPUnit='副', num='69.89', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='500012401', MaterialName='普通螺栓', Spec='普通螺栓,M16,70mm,铁,镀锌,配螺母', ERPUnit='只', num='', TechnicalProtocol='24GH-500012401-00001', BomType=''},
        BomBean{MaterialCodeERP='500065920', MaterialName='钢绞线', Spec='钢绞线,1×7-7.8-1270-B,35,镀锌', ERPUnit='吨', num='', TechnicalProtocol='9906-500059246-00003', BomType=''},
        BomBean{MaterialCodeERP='500028510', MaterialName='电缆挂钩', Spec='电缆挂钩,#50', ERPUnit='只', num='', TechnicalProtocol='24GH-500028510-00001', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='电缆绑扎线', Spec='BV-2.5mm²', ERPUnit='米', num='', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500020760',
            MaterialName='拉线金具-钢线卡子', Spec='拉线金具-钢线卡子,JK-1', ERPUnit='只', num='', TechnicalProtocol='9906-500020383-00004', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='有眼拉攀', Spec='—10×40×370', ERPUnit='副', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='500039274', MaterialName='膨胀螺栓', Spec='膨胀螺栓,M12,100mm,镀锌', ERPUnit='只', num='', TechnicalProtocol='24GH-500039274-00001', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='C0花篮螺栓', Spec='Φ8', ERPUnit='只', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='500132452', MaterialName='低压电力电缆', Spec='低压电力电缆,YJLV,铝,95,4芯,ZC,无铠装,普通', ERPUnit='千米', num='', TechnicalProtocol='9906-500108894-00001', BomType=''},
        BomBean{MaterialCodeERP='500129308', MaterialName='并沟线夹', Spec='接续金具-楔型并沟线夹,JXD-1', ERPUnit='只', num='', TechnicalProtocol='9906-500020383-00004', BomType=''},
        BomBean{MaterialCodeERP='500021802', MaterialName='电缆接线端子', Spec='电缆接线端子,铜铝过渡,95mm2,单孔', ERPUnit='只', num='', TechnicalProtocol='24GH-500021802-00002', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×95,户内终端,冷缩,铝', ERPUnit='副', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='500033187', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×95,户外终端,冷缩,铝', ERPUnit='副', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='电缆抱箍', Spec='电缆抱箍,—6×60,D220,单电缆上杆', ERPUnit='副', num='', TechnicalProtocol='', BomType=''},
        BomBean{MaterialCodeERP='', MaterialName='电缆抱箍', Spec='电缆抱箍,—6×60,D240,单电缆上杆', ERPUnit='副', num='', TechnicalProtocol='', BomType=''},




*/
                //[MaterialsProject{MaterialsProjectID='38', MaterialName='架空绝缘导线', MaterialsTypeKey='JKDX', MaterialCodeERP='500014663', Spec='AC10kV,JKLYJ,240', Voltage='10kV', IsMain='1', IsLine='1', DesignUnit='米', ERPUnit='千米', Ratio='1000', Weight='946.1', TechnicalProtocol='9906-500059246-00006', FLAG='', IsDonor='null', WuliaoFormnue='null'},
        // MaterialsProject{MaterialsProjectID='2000', MaterialName='低压电力电缆', MaterialsTypeKey='', MaterialCodeERP='500132455', Spec='低压电力电缆,YJLV,铝,70,4芯,ZC,无铠装,普通', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='千米', ERPUnit='千米', Ratio='null', Weight='null', TechnicalProtocol='9906-500108894-00001', FLAG='', IsDonor='', WuliaoFormnue='L*1.1'},
        // MaterialsProject{MaterialsProjectID='2001', MaterialName='异型并沟线夹', MaterialsTypeKey='', MaterialCodeERP='', Spec='异型并沟线夹', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null', WuliaoFormnue='8'},
        // MaterialsProject{MaterialsProjectID='2002', MaterialName='电缆接线端子', MaterialsTypeKey='', MaterialCodeERP='500021801', Spec='电缆接线端子,铜铝过渡,70mm2,单孔', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='24GH-500021801-00002', FLAG='null', IsDonor='null', WuliaoFormnue='8'},
        // MaterialsProject{MaterialsProjectID='2003', MaterialName='35kV及以下电缆终端', MaterialsTypeKey='', MaterialCodeERP='', Spec='1kV电缆终端,4×70,户外终端,冷缩,铝', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='副', ERPUnit='副', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null', WuliaoFormnue='1'},
        // MaterialsProject{MaterialsProjectID='2004', MaterialName='35kV及以下电缆终端', MaterialsTypeKey='', MaterialCodeERP='500033187', Spec='1kV电缆终端,4×70,户内终端,冷缩,铝', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='副', ERPUnit='副', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null', WuliaoFormnue='1'},
        // MaterialsProject{MaterialsProjectID='2005', MaterialName='成套抱箍', MaterialsTypeKey='', MaterialCodeERP='', Spec='成套抱箍,—8×80,D200', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='副', ERPUnit='副', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null', WuliaoFormnue='1'},
        // MaterialsProject{MaterialsProjectID='2006', MaterialName='普通螺栓', MaterialsTypeKey='', MaterialCodeERP='500012401', Spec='普通螺栓,M16,70mm,铁,镀锌,配螺母', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='24GH-500012401-00001', FLAG='null', IsDonor='null', WuliaoFormnue='2'},
        // MaterialsProject{MaterialsProjectID='2007', MaterialName='钢绞线', MaterialsTypeKey='', MaterialCodeERP='500065920', Spec='钢绞线,1×7-7.8-1270-B,35,镀锌', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='吨', ERPUnit='吨', Ratio='null', Weight='null', TechnicalProtocol='9906-500059246-00003', FLAG='null', IsDonor='null', WuliaoFormnue='0.32*L'},
        // MaterialsProject{MaterialsProjectID='2008', MaterialName='电缆挂钩', MaterialsTypeKey='', MaterialCodeERP='500028510', Spec='电缆挂钩,#50', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='24GH-500028510-00001', FLAG='null', IsDonor='null', WuliaoFormnue='L*1000*2'},
        // MaterialsProject{MaterialsProjectID='2009', MaterialName='电缆绑扎线', MaterialsTypeKey='', MaterialCodeERP='', Spec='BV-2.5mm²', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='米', ERPUnit='米', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null', WuliaoFormnue='2'},

        //TODO 2.0这里去ModuleMaterials查找根据MaterialsProjectID
        List<ModuleMaterials> moduleMaterials = getModuleMaterialsIDs(line);
        Log.e("resultt1", moduleMaterials.toString());

        //TODO 3.0这里去 MaterialsProject 表里面查询
        List<MaterialsProject> materialsProjects = getMaterialsProjectMessage(moduleMaterials);
        Log.e("resultt2", materialsProjects.toString());
        //TODO 4.物料清单
        List<BomBean> bomBeans = new ArrayList<>();
        for (int i = 0; i < materialsProjects.size(); i++) {
            BomBean bomBean = new BomBean();
            /*//物料编码
            private String MaterialCodeERP;
            //物料名称
            private String MaterialName;
            //物料规格信号
            private String Spec;
            //单位
            private String ERPUnit;
            //物料计算数量
            private String num;
            //物料计数协议
            private String TechnicalProtocol;
            //物料类型
            private String BomType="";*/
            bomBean.setMaterialCodeERP(materialsProjects.get(i).getMaterialCodeERP());
            bomBean.setMaterialName(materialsProjects.get(i).getMaterialName());
            bomBean.setSpec(materialsProjects.get(i).getSpec());
            bomBean.setERPUnit(materialsProjects.get(i).getERPUnit());
            //TODO 这里搞数据计算
            eval("10*1.1");
            //bomBean.setNum(materialsProjects.get(i).get());
            bomBean.setTechnicalProtocol(materialsProjects.get(i).getTechnicalProtocol());
            //TODO 类型
            //bomBean.setBomType("类型");

            bomBeans.add(bomBean);
        }
        Log.e("all", bomBeans.toString());
        //[BomBean{MaterialCodeERP='500132455', MaterialName='低压电力电缆', Spec='低压电力电缆,YJLV,铝,70,4芯,ZC,无铠装,普通', ERPUnit='千米', num='null', TechnicalProtocol='9906-500108894-00001', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='异型并沟线夹', Spec='异型并沟线夹', ERPUnit='只', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500021801', MaterialName='电缆接线端子', Spec='电缆接线端子,铜铝过渡,70mm2,单孔', ERPUnit='只', num='null', TechnicalProtocol='24GH-500021801-00002', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×70,户外终端,冷缩,铝', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500033187', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×70,户内终端,冷缩,铝', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='成套抱箍', Spec='成套抱箍,—8×80,D200', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500012401', MaterialName='普通螺栓', Spec='普通螺栓,M16,70mm,铁,镀锌,配螺母', ERPUnit='只', num='null', TechnicalProtocol='24GH-500012401-00001', BomType=''}, BomBean{MaterialCodeERP='500065920', MaterialName='钢绞线', Spec='钢绞线,1×7-7.8-1270-B,35,镀锌', ERPUnit='吨', num='null', TechnicalProtocol='9906-500059246-00003', BomType=''}, BomBean{MaterialCodeERP='500028510', MaterialName='电缆挂钩', Spec='电缆挂钩,#50', ERPUnit='只', num='null', TechnicalProtocol='24GH-500028510-00001', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='电缆绑扎线', Spec='BV-2.5mm²', ERPUnit='米', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500020760', MaterialName='拉线金具-钢线卡子', Spec='拉线金具-钢线卡子,JK-1', ERPUnit='只', num='null', TechnicalProtocol='9906-500020383-00004', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='有眼拉攀', Spec='—10×40×370', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500039274', MaterialName='膨胀螺栓', Spec='膨胀螺栓,M12,100mm,镀锌', ERPUnit='只', num='null', TechnicalProtocol='24GH-500039274-00001', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='C0花篮螺栓', Spec='Φ8', ERPUnit='只', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500014663', MaterialName='架空绝缘导线', Spec='AC10kV,JKLYJ,240', ERPUnit='千米', num='null', TechnicalProtocol='9906-500059246-00006', BomType=''}, BomBean{MaterialCodeERP='500132452', MaterialName='低压电力电缆', Spec='低压电力电缆,YJLV,铝,95,4芯,ZC,无铠装,普通', ERPUnit='千米', num='null', TechnicalProtocol='9906-500108894-00001', BomType=''}, BomBean{MaterialCodeERP='500129308', MaterialName='并沟线夹', Spec='接续金具-楔型并沟线夹,JXD-1', ERPUnit='只', num='null', TechnicalProtocol='9906-500020383-00004', BomType=''}, BomBean{MaterialCodeERP='500021802', MaterialName='电缆接线端子', Spec='电缆接线端子,铜铝过渡,95mm2,单孔', ERPUnit='只', num='null', TechnicalProtocol='24GH-500021802-00002', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×95,户内终端,冷缩,铝', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='500033187', MaterialName='35kV及以下电缆终端', Spec='1kV电缆终端,4×95,户外终端,冷缩,铝', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='电缆抱箍', Spec='电缆抱箍,—6×60,D220,单电缆上杆', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='电缆抱箍', Spec='电缆抱箍,—6×60,D240,单电缆上杆', ERPUnit='副', num='null', TechnicalProtocol='', BomType=''}, BomBean{MaterialCodeERP='', MaterialName='电


        //[MaterialsProject{MaterialsProjectID='68', MaterialName='高压电力电缆', MaterialsTypeKey='DL', MaterialCodeERP='500108478', Spec='AC10kV,YJV,70,3,22,ZC,无阻水', Voltage='10kV', IsMain='1', IsLine='1', DesignUnit='千米', ERPUnit='千米', Ratio='1000', Weight='3508', TechnicalProtocol='9906-500030091-00003', FLAG='', IsDonor='null'},
        //MaterialsProject{MaterialsProjectID='38', MaterialName='架空绝缘导线', MaterialsTypeKey='JKDX', MaterialCodeERP='500014663', Spec='AC10kV,JKLYJ,240', Voltage='10kV', IsMain='1', IsLine='1', DesignUnit='米', ERPUnit='千米', Ratio='1000', Weight='946.1', TechnicalProtocol='9906-500059246-00006', FLAG='', IsDonor='null'}]
//[MaterialsProject{MaterialsProjectID='2000', MaterialName='低压电力电缆', MaterialsTypeKey='', MaterialCodeERP='500132455', Spec='低压电力电缆,YJLV,铝,70,4芯,ZC,无铠装,普通', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='千米', ERPUnit='千米', Ratio='null', Weight='null', TechnicalProtocol='9906-500108894-00001', FLAG='', IsDonor=''},
// MaterialsProject{MaterialsProjectID='2001', MaterialName='异型并沟线夹', MaterialsTypeKey='', MaterialCodeERP='', Spec='异型并沟线夹', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null'},
// MaterialsProject{MaterialsProjectID='2002', MaterialName='电缆接线端子', MaterialsTypeKey='', MaterialCodeERP='500021801', Spec='电缆接线端子,铜铝过渡,70mm2,单孔', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='24GH-500021801-00002', FLAG='null', IsDonor='null'},
// MaterialsProject{MaterialsProjectID='2003', MaterialName='35kV及以下电缆终端', MaterialsTypeKey='', MaterialCodeERP='', Spec='1kV电缆终端,4×70,户外终端,冷缩,铝', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='副', ERPUnit='副', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null'},
// MaterialsProject{MaterialsProjectID='2004', MaterialName='35kV及以下电缆终端', MaterialsTypeKey='', MaterialCodeERP='500033187', Spec='1kV电缆终端,4×70,户内终端,冷缩,铝', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='副', ERPUnit='副', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null'},
//MaterialsProject{MaterialsProjectID='2005', MaterialName='成套抱箍', MaterialsTypeKey='', MaterialCodeERP='', Spec='成套抱箍,—8×80,D200', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='副', ERPUnit='副', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null'}, MaterialsProject{MaterialsProjectID='2006', MaterialName='普通螺栓', MaterialsTypeKey='', MaterialCodeERP='500012401', Spec='普通螺栓,M16,70mm,铁,镀锌,配螺母', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='24GH-500012401-00001', FLAG='null', IsDonor='null'}, MaterialsProject{MaterialsProjectID='2007', MaterialName='钢绞线', MaterialsTypeKey='', MaterialCodeERP='500065920', Spec='钢绞线,1×7-7.8-1270-B,35,镀锌', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='吨', ERPUnit='吨', Ratio='null', Weight='null', TechnicalProtocol='9906-500059246-00003', FLAG='null', IsDonor='null'}, MaterialsProject{MaterialsProjectID='2008', MaterialName='电缆挂钩', MaterialsTypeKey='', MaterialCodeERP='500028510', Spec='电缆挂钩,#50', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='24GH-500028510-00001', FLAG='null', IsDonor='null'}, MaterialsProject{MaterialsProjectID='2009', MaterialName='电缆绑扎线', MaterialsTypeKey='', MaterialCodeERP='', Spec='BV-2.5mm²', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='米', ERPUnit='米', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null'}, MaterialsProject{MaterialsProjectID='2010', MaterialName='拉线金具-钢线卡子', MaterialsTypeKey='', MaterialCodeERP='500020760', Spec='拉线金具-钢线卡子,JK-1', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='只', ERPUnit='只', Ratio='null', Weight='null', TechnicalProtocol='9906-500020383-00004', FLAG='null', IsDonor='null'}, MaterialsProject{MaterialsProjectID='2011', MaterialName='有眼拉攀', MaterialsTypeKey='', MaterialCodeERP='', Spec='—10×40×370', Voltage='0.4kV', IsMain='null', IsLine='null', DesignUnit='副', ERPUnit='副', Ratio='null', Weight='null', TechnicalProtocol='', FLAG='null', IsDonor='null'}, MaterialsProject{Material

    }

    private List<MaterialsProject> getReulst(List<Line> line) {
        List<MaterialsProject> strings = new ArrayList<>();
        SQLiteDatabase db = openDatabase(this.getContext());
        List<String> resultInt = new ArrayList<>();
        String end;
        StringBuilder builder = new StringBuilder("in (");
        for (int i = 0; i < line.size(); i++) {
            if (line.get(i).getModuleID() != null && !line.get(i).getModuleID().isEmpty()) {
                resultInt.add(line.get(i).getModuleID());
            }
        }
        for (int i = 0; i < resultInt.size(); i++) {
            if (i < resultInt.size() - 1) {
                builder.append(line.get(i).getModuleID());
                builder.append(",");
            } else {
                builder.append(line.get(i).getModuleID());
                builder.append(")");
            }
        }
        end = builder.toString();
        Log.e("end", end);
        assert db != null;
        //select mp.*, mm.Formula from ModuleMaterials mm left join MaterialsProject mp on mm.MaterialsProjectID = mp.MaterialsProjectID where mm.ModuleID in (2000,350,2005);
        @SuppressLint("Recycle")
        // Cursor cursor = db.rawQuery("select mp.*,mm.Formula from ModuleMaterials mm left join MaterialsProject mp on mm.MaterialsProjectID = mp.MaterialsProjectID where mm.ModuleID in (2000,350,2005)", null);//此处的db是表名，name是列名
                Cursor cursor = db.rawQuery("select mp.*,mm.Formula from ModuleMaterials mm left join MaterialsProject mp on mm.MaterialsProjectID = mp.MaterialsProjectID where mm.ModuleID " + end, null);//此处的db是表名，name是列名

        while (cursor.moveToNext()) {
            MaterialsProject modelType = new MaterialsProject();
            modelType.setMaterialsProjectID(cursor.getString(0));
            modelType.setMaterialName(cursor.getString(1));
            modelType.setMaterialsTypeKey(cursor.getString(2));
            modelType.setMaterialCodeERP(cursor.getString(3));
            modelType.setSpec(cursor.getString(4));
            modelType.setVoltage(cursor.getString(5));
            modelType.setIsMain(cursor.getString(6));
            modelType.setIsLine(cursor.getString(7));
            modelType.setDesignUnit(cursor.getString(8));
            modelType.setERPUnit(cursor.getString(9));
            modelType.setRatio(cursor.getString(10));
            modelType.setWeight(cursor.getString(11));
            modelType.setTechnicalProtocol(cursor.getString(12));
            modelType.setFLAG(cursor.getString(13));
            modelType.setIsDonor(cursor.getString(14));
            modelType.setWuliaoFormnue(cursor.getString(15));
            strings.add(modelType);
        }
        cursor.close();
        db.close();
        return strings;
    }

    /**
     * 这里去ModuleMaterials查找根据MaterialsProjectID
     *
     * @paramline 所有线的集合
     */
    private List<MaterialsProject> getMaterialsProjectMessage(List<ModuleMaterials> moduleMaterial) {
        List<MaterialsProject> strings = new ArrayList<>();
        SQLiteDatabase db = openDatabase(this.getContext());
        assert db != null;
        for (int i = 0; i < moduleMaterial.size(); i++) {
            if (moduleMaterial.get(i).getMaterialsProjectID() != null) {
                @SuppressLint("Recycle")
                Cursor cursor = db.rawQuery("select * from MaterialsProject where MaterialsProjectID=" + moduleMaterial.get(i).getMaterialsProjectID(), null);//此处的db是表名，name是列名
                while (cursor.moveToNext()) {
                    MaterialsProject modelType = new MaterialsProject();
                    modelType.setMaterialsProjectID(cursor.getString(0));
                    modelType.setMaterialName(cursor.getString(1));
                    modelType.setMaterialsTypeKey(cursor.getString(2));
                    modelType.setMaterialCodeERP(cursor.getString(3));
                    modelType.setSpec(cursor.getString(4));
                    modelType.setVoltage(cursor.getString(5));
                    modelType.setIsMain(cursor.getString(6));
                    modelType.setIsLine(cursor.getString(7));
                    modelType.setDesignUnit(cursor.getString(8));
                    modelType.setERPUnit(cursor.getString(9));
                    modelType.setRatio(cursor.getString(10));
                    modelType.setWeight(cursor.getString(11));
                    modelType.setTechnicalProtocol(cursor.getString(12));
                    modelType.setFLAG(cursor.getString(13));
                    modelType.setIsDonor(cursor.getString(14));
                    strings.add(modelType);
                }
                cursor.close();
            }
        }
        db.close();
        return strings;
    }

    /**
     * 这里去ModuleMaterials查找根据MaterialsProjectID
     *
     * @param line 所有线的集合
     */
    private List<ModuleMaterials> getModuleMaterialsIDs(List<Line> line) {
        List<ModuleMaterials> strings = new ArrayList<>();
        SQLiteDatabase db = openDatabase(this.getContext());
        assert db != null;
        for (int i = 0; i < line.size(); i++) {
            if (line.get(i).getModuleID() != null) {
                @SuppressLint("Recycle")
                Cursor cursor = db.rawQuery("select * from ModuleMaterials where ModuleID=" + line.get(i).getModuleID(), null);//此处的db是表名，name是列名
                while (cursor.moveToNext()) {
                    ModuleMaterials modelType = new ModuleMaterials();
                    modelType.setMaterialsProjectID(cursor.getString(0));
                    modelType.setModuleID(cursor.getString(1));
                    modelType.setModuleMaterialsID(cursor.getString(2));
                    modelType.setNum(cursor.getString(3));
                    modelType.setMustNeed(cursor.getString(4));
                    modelType.setFormula(cursor.getString(5));
                    strings.add(modelType);
                }
                cursor.close();
            }
        }
        db.close();
        return strings;
    }

    public static double eval(final String str) {
        return new Object() {
            int pos = -1, ch;

            void nextChar() {
                ch = (++pos < str.length()) ? str.charAt(pos) : -1;
            }

            boolean eat(int charToEat) {
                while (ch == ' ') nextChar();
                if (ch == charToEat) {
                    nextChar();
                    return true;
                }
                return false;
            }

            double parse() {
                nextChar();
                double x = parseExpression();
                if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char) ch);
                return x;
            }

            // Grammar:
            // expression = term | expression `+` term | expression `-` term
            // term = factor | term `*` factor | term `/` factor
            // factor = `+` factor | `-` factor | `(` expression `)`
            //        | number | functionName factor | factor `^` factor

            double parseExpression() {
                double x = parseTerm();
                for (; ; ) {
                    if (eat('+')) x += parseTerm(); // addition
                    else if (eat('-')) x -= parseTerm(); // subtraction
                    else return x;
                }
            }

            double parseTerm() {
                double x = parseFactor();
                for (; ; ) {
                    if (eat('*')) x *= parseFactor(); // multiplication
                    else if (eat('/')) x /= parseFactor(); // division
                    else return x;
                }
            }

            double parseFactor() {
                if (eat('+')) return parseFactor(); // unary plus
                if (eat('-')) return -parseFactor(); // unary minus

                double x;
                int startPos = this.pos;
                if (eat('(')) { // parentheses
                    x = parseExpression();
                    eat(')');
                } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
                    while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
                    x = Double.parseDouble(str.substring(startPos, this.pos));
                } else if (ch >= 'a' && ch <= 'z') { // functions
                    while (ch >= 'a' && ch <= 'z') nextChar();
                    String func = str.substring(startPos, this.pos);
                    x = parseFactor();
                    if (func.equals("sqrt")) x = Math.sqrt(x);
                    else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
                    else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
                    else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
                    else throw new RuntimeException("Unknown function: " + func);
                } else {
                    throw new RuntimeException("Unexpected: " + (char) ch);
                }

                if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation

                return x;
            }
        }.parse();
    }

}
